/**
 * $Id: LoginFailureHandler.java 19 2011-06-11 04:06:06Z feigme $
 */
package com.iary.interfaces.security;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

public class LoginFailureHandler implements AuthenticationFailureHandler {

	private String adminFailureUrl;
	private String onlineFailureUrl;

	public void onAuthenticationFailure(HttpServletRequest request,
			HttpServletResponse response, AuthenticationException exception)
			throws IOException, ServletException {
		String targetUrl;
		String type = (String) request.getSession().getAttribute("type");
		if (type != null && type.equals("admin")) {
			targetUrl = adminFailureUrl;
		} else {
			targetUrl = onlineFailureUrl;
		}
		response.sendRedirect(request.getContextPath() + targetUrl);
	}

	public String getAdminFailureUrl() {
		return adminFailureUrl;
	}

	public void setAdminFailureUrl(String adminFailureUrl) {
		this.adminFailureUrl = adminFailureUrl;
	}

	public String getOnlineFailureUrl() {
		return onlineFailureUrl;
	}

	public void setOnlineFailureUrl(String onlineFailureUrl) {
		this.onlineFailureUrl = onlineFailureUrl;
	}

}
